<?php
class Pageley_Domain_Validator_NumberValue extends Pageley_Domain_Validator_Number {
	private $minValue = -999999999;
	private $maxValue = 999999999;
	private $tooLargeMessage = "error.validation.tooLarge";
	private $tooSmallMessage = "error.validation.tooSmall";
	private $notAnIntegerMessage = "error.validation.notAnInteger";
	private $integer = false;

	public function setMinValue($minValue) {
		$this->minValue = $minValue;
	}

	public function setMaxValue($maxValue) {
		$this->maxValue = $maxValue;
	}

	public function setTooLargeMessage($tooLargeMessage) {
		$this->tooLargeMessage = $tooLargeMessage;
	}

	public function setTooSmallMessage($tooSmallMessage) {
		$this->tooSmallMessage = $tooSmallMessage;
	}
	
	public function setNotAnIntegerMessage($notAnIntegerMessage) {
		$this->notAnIntegerMessage = $notAnIntegerMessage;
	}
	
	public function setInteger($integer) {
		$this->integer = $integer;
	}

	public function validate($value, $propertyName, $object) {
		$errors = new Pageley_Util_ArrayMap();
        if($value !== null && $value !== "") {
    		$errors->putAll(parent::validate($value, $propertyName, $object));
    		if($errors->size() === 0) {
    			$tooSmall = $this->rejectIfSmaller($value, $this->minValue, $propertyName, $this->tooSmallMessage);
    			if($tooSmall) {
    				$errors->put($propertyName, $tooSmall);
    			}
    			$tooLarge = $this->rejectIfGreater($value, $this->maxValue, $propertyName, $this->tooLargeMessage);
    			if($tooLarge) {
    				$errors->put($propertyName, $tooLarge);
    			}
    			if($this->integer) {
    				$notAnInteger = $this->rejectIfFalse(((string) $value) === ((string)(int) $value) , $this->notAnIntegerMessage, array("{field." . $propertyName . "}", $value));
    				if($notAnInteger) {
    		            $errors->put($propertyName, $notAnInteger);
    		        }
    			}
    		}
        }
		return $errors;
	}
}